<?php

namespace lc\wechat\wxpay\base;

use lc\helpers\Helper;
use lc\wechat\wxpay\database\RedPack;
use lc\wechat\wxpay\database\Results;
use lc\wechat\wxpay\database\Transfer;
use lc\wechat\wxpay\database\UnifyOrder;
use think\facade\Config;
use think\facade\Db;

class WxPayHelper
{
    /**
     * @param Transfer|RedPack $database 红包类|企业支付零钱类
     * @param array $output
     * @param int 	$type
     */
    public static function writeRedPackLogs($database, array $output, int $type)
    {
        if ($type == 1) {
			$paymentNo 	= $output['payment_no'] ?? '';
			$openid		= $database->getAttributes('openid');
			$orderId	= $database->getAttributes('partner_trade_no');
			$amount		= $database->getAttributes('amount');
		} else {
			$paymentNo 	= $output['send_listid'] ?? '';
			$openid		= $database->getAttributes('re_openid');
			$orderId	= $database->getAttributes('mch_billno');
			$amount		= $database->getAttributes('total_amount');
		}
        Db::connect('main')
            ->table('hblogs')
            ->insert([
                'h_appid'       => Config::get('lc.wxpay.id'),
                'h_env'         => 'linuxbalancedLoad',
                'h_proj_id'     => env('id', Config::get('lc.id')),
                'h_openid'      => $openid,
                'h_order_id'    => $orderId,
                'h_amount'      => $amount,
                'h_payment_no'  => $paymentNo,
                'h_input'       => json_encode($database->getValues(), 256),
                'h_output'      => json_encode($output, 256),
                'h_result_code' => isset($output['result_code']) ? $output['result_code'] : $output['return_code'],
                'h_err_code'    => isset($output['err_code']) ? $output['err_code'] : '',
                'h_created'     => date('YmdHis'),
            ]);
    }

    /**
     * @desc    记录调用日志
     * @param   UnifyOrder  $unify      统一下单类
     * @param   array       $output     接口返回数据
     * @param   string      $reqName    请求名
     */
    public static function writeWxOrderLogs($unify, array $output, string $reqName)
    {
        Db::table('lottery_wxorder_logs')
            ->insert([
                'ol_req_name'    => $reqName,
                'ol_appid'       => Config::get('lc.wxpay.id'),
                'ol_env'         => 'linuxbalancedLoad',
                'ol_proj_id'     => env('id', Config::get('lc.id')),
                'ol_openid'      => $unify->getAttributes('openid'),
                'ol_order_id'    => $unify->getAttributes('out_trade_no'),
                'ol_amount'      => $unify->getAttributes('total_fee') ? $unify->getAttributes('total_fee') : 0,
                'ol_input'       => json_encode($unify->getValues(), 256),
                'ol_output'      => json_encode($output, 256),
                'ol_result_code' => isset($output['result_code']) ? $output['result_code'] : $output['return_code'],
                'ol_err_code'    => isset($output['err_code']) ? $output['err_code'] : '',
                'ol_created'     => date('YmdHis'),
            ]);
    }

    /*
     * @param   int     $type   1企业支付到零钱，2现金红包
     * @param   string  $url    请求地址
     * @param   array   $params 请求参数
     * @return  array
     */
    public static function sendRedPackRequest(int $type, string $url, array $params)
    {
        $class = ($type == 1) ? new Transfer() : new RedPack();
        foreach ($params as $key => $value) {
            $class->setAttributes($key, $value);
        }
        $class->setSign();

        $xml        = Helper::arrayToXml($class->getValues());
        $response   = self::postXmlCurl($xml, $url, true, 6);
        $result     = Results::init($response, false);

        self::writeRedPackLogs($class, $result, $type);
        return $result;
    }

    /*
     * @param   int     $type   查询红包详情
     * @param   string  $url    请求地址
     * @param   array   $params 请求参数
     * @return  array
     */
    public static function queryRequest(int $type, string $url, array $params)
    {
        $class   = ($type == 1) ? new Transfer() : new RedPack();
        $apiName = ($type == 1) ? 'gettransferinfo' : 'gethbinfo';
        foreach ($params as $key => $value) {
            $class->setAttributes($key, $value);
        }
        $class->setSign();

        $xml        = Helper::arrayToXml($class->getValues());
        $response   = self::postXmlCurl($xml, $url, true, 6);
        $result     = Results::init($response, false);

        self::writeWxOrderLogs($class, $result, $apiName);
        return $result;
    }

    /*
     * @desc    发送统一下单请求
     * @param   string  $url        请求地址
     * @param   array   $params     参数
     * @param   string  $apiName    接口名
     * @return  array
     */
    public static function sendUnifyRequest(string $url, array $params, string $apiName)
    {
        $unify = new UnifyOrder();
        foreach ($params as $key => $value) {
            $unify->setAttributes($key, $value);
        }
        $unify->setSign();

        $xml        = Helper::arrayToXml($unify->getValues());
        $response   = self::postXmlCurl($xml, $url, in_array($apiName, ['refund', 'send_coupon']) ? true : false, 6);
        $result     = Results::init($response, false);

        self::writeWxOrderLogs($unify, $result, $apiName);
        return $result;
    }

    /**
     * @desc    以post方式提交xml到对应的接口url
     * @param   string  $xml        需要post的xml数据
     * @param   string  $url        url
     * @param   bool    $useCert    是否需要证书，默认不需要
     * @param   int     $second     url执行超时时间，默认30s
     * @return  mixed
     * @throws  WxPayException
     */
    public static function postXmlCurl($xml, $url, $useCert = false, $second = 30)
    {
        $ch = curl_init();
        // 设置超时
        curl_setopt($ch, CURLOPT_TIMEOUT, $second);

        // 如果有配置代理这里就设置代理
        if (WxPayConfig::$curlProxyPort != '0.0.0.0' && WxPayConfig::$curlProxyPort != 0) {
            curl_setopt($ch, CURLOPT_PROXY, WxPayConfig::$curlProxyHost);
            curl_setopt($ch, CURLOPT_PROXYPORT, WxPayConfig::$curlProxyPort);
        }
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);// 严格校验
        // 设置header
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        // 要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

        if ($useCert == true) {
            // 设置证书
            // 使用证书：cert 与 key 分别属于两个.pem文件
            curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');
            curl_setopt($ch, CURLOPT_SSLCERT, WxPayConfig::$sslCertPath);
            curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');
            curl_setopt($ch, CURLOPT_SSLKEY, WxPayConfig::$sslKeyPath);
        }
        // post提交方式
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
        // 运行curl
        $data = curl_exec($ch);
        // 返回结果
        if ($data) {
            curl_close($ch);
            return $data;
        } else {
            $errorNo = curl_errno($ch);
            $error   = curl_error($ch);
            curl_close($ch);
            throw new WxPayException('curl出错，错误码: ' . $errorNo . '，错误信息：' . $error);
        }
    }

    /**
     * @desc    获取 JSAPI 支付，前端所需参数数据
     * @param   string  $prepayId   统一下单成功返回的id
     * @return  array
     */
    public static function getJsApiParameters($prepayId)
    {
        new WxPayConfig();
        $data = [
            'appId'     => WxPayConfig::$appId,
            'timeStamp' => (string) time(),
            'nonceStr'  => Helper::generateNonceStr(),
            'package'   => 'prepay_id=' . $prepayId,
            'signType'  => 'MD5',
        ];

        $data['paySign'] = Helper::makeSign($data, WxPayConfig::$key);
        return $data;
    }
}